package com.hualude.roadsign.controller;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.JsonMsgDto;
import com.hualude.roadsign.dto.MonitorDto;
import com.hualude.roadsign.service.MonitorService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by xu_jiaHui on 2017/8/8.
 */
@Controller
@RequestMapping("/rds_MonitorManage")
public class MonitorController {
    @Resource
    private MonitorService monitorService;

    @RequestMapping("/getMonitor4DT")
    @ResponseBody
    public DTResponseDto getMonitor4DT(DTRequestDto dtRequestDto,
                                       String actorIds,
                                       Integer orgId,
                                       Boolean audit,
                                       String startRoad,
                                       String endRoad,
                                       String roadName,
                                       Integer alarmBinding) throws Exception {
        DTResponseDto dtResponseDto = new DTResponseDto();
        try {
            dtResponseDto = monitorService.getMonitor4DT(
                    dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName, alarmBinding);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dtResponseDto;
    }

    @RequestMapping("/deleteMonitor")
    @ResponseBody
    public JsonMsgDto deleteMonitor(String monitorIds) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(monitorService.deleteMonitor(monitorIds));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/addMonitor")
    @ResponseBody
    public JsonMsgDto addMonitor(MonitorDto monitorDto) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(monitorService.addMonitor(monitorDto));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/updateMonitor")
    @ResponseBody
    public JsonMsgDto updateMonitor(MonitorDto monitorDto) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(monitorService.updateMonitor(monitorDto));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getStartRoadByRoadName")
    @ResponseBody
    public JsonMsgDto getStartRoadByRoadName(String roadName, String actorIds, Integer orgId) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(monitorService.getStartRoadByRoadName(roadName, actorIds, orgId));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getEndRoadByStartRoad")
    @ResponseBody
    public JsonMsgDto getEndRoadByStartRoad(String city, String monitorRoadName, String startRoad, String actorIds, Integer orgId) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(monitorService.getEndRoadByStartRoad(city, monitorRoadName, startRoad, actorIds, orgId));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getMonitor4APP")
    @ResponseBody
    public JsonMsgDto getMonitor4APP(String actorIds,
                                     Integer orgId,
                                     Integer mCount,
                                     Integer mSize,
                                     String city,
                                     String startRoad,
                                     String endRoad,
                                     String roadName,
                                     String alarmType) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(monitorService.getMonitor4APP(
                    actorIds, orgId, mCount, mSize, city, startRoad, endRoad, roadName, alarmType));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMsgDto.setIsSuccess(false);
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getMonitorById")
    @ResponseBody
    public JsonMsgDto getMonitorById(Integer monitorId) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(monitorService.getMonitorById(monitorId));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMsgDto.setIsSuccess(false);
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getCity4Monitor4App")
    @ResponseBody
    public JsonMsgDto getCity4Monitor4App(String actorIds, Integer orgId, Integer mCount, Integer mSize) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(monitorService.getCity4Monitor4App(actorIds, orgId, mCount, mSize));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getRoadName4MonitorByCity")
    @ResponseBody
    public JsonMsgDto getRoadName4MonitorByCity(String actorIds, Integer orgId, String city, Integer mCount, Integer mSize) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(monitorService.getRoadName4MonitorByCity(actorIds, orgId, city, mCount, mSize));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getStartRoadByRoadName4App")
    @ResponseBody
    public JsonMsgDto getStartRoadByRoadName4App(String city, String roadName, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(monitorService.getStartRoadByRoadName4App(city, roadName, actorIds, orgId, mCount, mSize));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getEndRoadByStartRoad4App")
    @ResponseBody
    public JsonMsgDto getEndRoadByStartRoad4App(String city, String monitorRoadName, String startRoad, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(monitorService.getEndRoadByStartRoad4App(city, monitorRoadName, startRoad, actorIds, orgId, mCount, mSize));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/getMonitor4Map4App")
    @ResponseBody
    public JsonMsgDto getMonitor4Map4App(Integer orgId,
                                         String actorIds,
                                         double neLongitude,
                                         double neLatitude,
                                         double swLongitude,
                                         double swLatitude) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(monitorService.getMonitor4Map4App(orgId, actorIds,
                    neLongitude, neLatitude, swLongitude, swLatitude));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping("/auditMonitor")
    @ResponseBody
    public JsonMsgDto auditMonitor(String monitorIds) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(monitorService.auditMonitor(monitorIds));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }


    /**
     * Created by WYX on 2018/4/29.
     */

    @ResponseBody
    @RequestMapping("/bindingDevice")
    public JsonMsgDto bindingDevice(String deviceId, Integer monitorId) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(monitorService.bindingDevice(deviceId, monitorId));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @ResponseBody
    @RequestMapping("/unbindingDevice")
    public JsonMsgDto unbindingDevice(String deviceId, Integer monitorId) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(monitorService.unbindingDevice(deviceId, monitorId));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
            e.printStackTrace();
        }
        return jsonMsgDto;
    }


    @ResponseBody
    @RequestMapping("/showAlarmHistory")
    public DTResponseDto showAlarmHistory(DTRequestDto dtRequestDto, String deviceId, Integer deviceType) {
        DTResponseDto dtResponseDto = null;
        try {
            dtResponseDto = monitorService.showAlarmHistory(dtRequestDto, deviceId, deviceType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dtResponseDto;
    }
}
